<?php

namespace App\Admin\Forms;

use App\Models\User;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Contracts\LazyRenderable;

class IcenClass extends Form implements LazyRenderable
{
    use LazyWidget;
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return Response
     */
    public function handle(array $input)
    {
        // dump($input);
        // 获取外部传递参数
        $period = $input['period'] ?? null;
        $cause = $input['cause'] ?? null;

        $user_id = $this->payload['id'] ?? null;
        //查询此人身份
        $level = User::where('id',$user_id)->value('level');
        if ($level != 1){
            return $this->error('此人不是球员', '/User');
        }
        DB::beginTransaction();
        //更新
        $res = User::where('id',$user_id)->increment('period',$period);
        //加入增加记录表
        $res1 = DB::table('period_recode')->insert([
             'user_id'=>$user_id,
             'period'=>$period,
             'cause'=>$cause,
             'created_at'=>time()
         ]);
        if ($res && $res1){
            DB::commit();
            return $this->success('增加成功', '/User');
        }else{
            DB::rollBack();
            return $this->error('增加失败', '/User');
        }
    }
    /**
     * Build a form here.
     */
    public function form()
    {
        $this->text('period','课时时长');
        $this->text('cause','理由');
    }

}
